package projectx.core.base;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;

public class ProjectXMessage {

	private static final String UNKNOWN_KEY = "Unknown message key [{0}]";

	private static final String NOT_INITIALIZED = "[{0}] is not loaded. key=[{1}], value=[{2}]";

	private static final Logger _log = Logger.getLogger(ProjectXMessage.class
			.getName());

	private static boolean _initialized;

	private static Properties _prop;

	private static String _fileName;
	
	private static Map multiRowMsgKeyMapping=null;

	public static void initialize(String fileName)
			throws ProjectXSystemException {

		if (!_initialized) {

			synchronized (ProjectXMessage.class) {

				if (!_initialized) {

					_fileName = fileName;

					_log.info("Initialize using [" + _fileName + "]...");

					_prop = ProjectXProperties.load(_fileName);

					_log.info("Initialized successfully");

					_initialized = true;
				}
				
				initMultiRowMsgKeyMapping();
			}
		}
	}
	
	private static void initMultiRowMsgKeyMapping(){
        multiRowMsgKeyMapping = new HashMap();
        multiRowMsgKeyMapping.put("E05017","E05350");
	}

	public static boolean isInitialized() {
		return _initialized;
	}
	private/* final */String _key;
	
	private/* final */Object[] _values;

	public ProjectXMessage(String key) {

		this(key, null);
	}

	public ProjectXMessage(String key, Object value0) {

		this(key, new Object[] { value0 });
	}

	public ProjectXMessage(String key, Object value0, Object value1) {

		this(key, new Object[] { value0, value1 });
	}

	public ProjectXMessage(String key, Object value0, Object value1,
			Object value2) {

		this(key, new Object[] { value0, value1, value2 });
	}

	public ProjectXMessage(String key, Object[] values) {

		_key = key;
		_values = values;
	}

	public String getKey() {
		return _key;
	}

	public Object[] getValues() {
		return _values;
	}

	public String getMessage() {

		return getMessage(true, null);
	}

	public String getMessage(boolean displayKey){
		return getMessage( displayKey, null );
	}
	
	public String getMessage(boolean displayKey, String multiRowMsg) {

		if (_initialized) {

			String msg;
			Object[] values;

			if (multiRowMsg != null) {
				_key = makeMultiRowMsgKey(_key);
				_values = makeMultiRowMsgValues(_values, multiRowMsg);
			}

			msg = _prop.getProperty(_key);
			values = _values;

			if (msg == null) {
				msg = UNKNOWN_KEY;
				values = new Object[] { _key };
			}

			String formattedMsg = MessageFormat.format(msg, values);

			return displayKey ? (_key + ": " + formattedMsg) : formattedMsg;
		} else {

			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < _values.length; i++) {
				if (i > 0) {
					sb.append(',');
				}
				sb.append(_values[i]);
			}

			return MessageFormat.format(NOT_INITIALIZED, new Object[] {
					_fileName, _key, sb });
		}
	}

	private static String makeMultiRowMsgKey(String msgKey) {
		if( msgKey != null && msgKey.length() > 3 ){
			
			if(multiRowMsgKeyMapping.containsKey( msgKey )){
				return (String)multiRowMsgKeyMapping.get( msgKey );
			}
			
			return msgKey.replaceFirst("E03","E053");
		}
		return msgKey;
	}

	private static Object[] makeMultiRowMsgValues(Object[] values, String multiRowMsg) {
		Object[] multiRowValues;
		if( values != null ){
			multiRowValues = new Object[values.length+1];
			System.arraycopy(values, 0, multiRowValues, 0, values.length);
		}else{
			multiRowValues = new Object[1];
		}

		multiRowValues[multiRowValues.length - 1] = multiRowMsg;

		return multiRowValues;
	}

	public String getMessage(String multiRowMessage) {

		return getMessage(true, multiRowMessage);
	}

	public String toString() {

		return getMessage();
	}
}
